const api = require("../../server/Api.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    sid: "",
    goods: {},
    refundOrder: {},
    quantity: 1,
    amount: 0,
    amount_jb: 0,
    type_name: "退款",
    img_arr: [], //存放上传的图片
    details: '', //详细的售后描述
    style: {}, //售后方式
    reason: {}, //售后原因
    reasons: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type === "tk") {
      this.setData({
        type_name: "退款",
        style: {
          type: 1
        },
        reasons: ["不想要了", "买错了", "太贵了", "提货太麻烦了", "其它"]
      })
    } else if (options.type === "thtk") {
      this.setData({
        type_name: "退货",
        style: {
          type: 2
        },
        reasons: ["7天无理由退货", "质量有问题", "太贵了", "售后服务态度糟糕", "实物和描述不相符", "买错了", "其它"]
      })
    }
    tt.setNavigationBarTitle({
      title: '申请' + this.data.type_name
    })
    let that = this;
    if (options.sid) {
      that.setData({
        sid: options.sid
      })
      api.refundDetail2(that.data.sid).then(res => {
        that.setData({
          refundOrder: res.refundOrder,
          id: res.refundOrder.subId,
          quantity: res.refundOrder.applyQtys,
          reason: {
            type: 1,
            name: res.refundOrder.reasons
          },
          details: res.refundOrder.remark || ""
        })
        for (let i = 0; i < res.listHistory.length; i++) {
          if (res.listHistory[i].status == 1) {
            res.listHistory[i].remark = JSON.parse(res.listHistory[i].remark)
            that.setData({
              img_arr: res.listHistory[i].remark.imgaes
            })
          }
        }
        that.getOrderInfo()
      }).catch(msg => {
        let errorMsg = typeof msg == "string" ? msg : msg.message
        console.log(errorMsg)
        tt.navigateBack()
        app.showToast("获取售后信息失败!")
      })
    } else if (options.id) {
      that.setData({
        id: options.id
      })
      that.getOrderInfo()
    }
  },
  getOrderInfo() {
    let that = this
    api.applyAfterSaleInfo(that.data.id).then(res => {
      console.log(res);
      res.image = app.resetImageUrl(res.image, 172, 172);
      let goods = {
        "goodsName": res.productTitle,
        "specName": res.specCn,
        "goodsCount": res.qty,
        "goodsMaxCount": res.applyMaxQty,
        "goodsImage": res.image,
        "goodsPrice": res.money
      }
      that.setData({
        goods: goods
      })
      that.setAmount();
    }).catch(msg => {
      let errorMsg = typeof msg == "string" ? msg : msg.message
      console.log(errorMsg)
      tt.navigateBack()
      app.showToast("获取商品信息失败!")
    })
  },
  //详细的原因描述
  details: function(e) {
    this.setData({
      details: e.detail.value
    })
  },

  // 图片上传
  addPhoto: function() {
    var that = this;
    if (this.data.img_arr.length < 4) {
      tt.chooseImage({
        count: 4 - this.data.img_arr.length,
        success: function(res) {
          api.refundUploadFile(res.tempFilePaths[0]).then(res => {
            that.setData({
              img_arr: that.data.img_arr.concat(res.url)
            })
          }).catch(msg => {
            app.showToast(msg)
          })
        }
      })
    } else {
      tt.showToast({
        title: '最多上传四张图片',
        icon: 'none',
        duration: 1000
      });
    }
  },
  //提交申请
  submit: function() {
    if (!this.data.reason.type) {
      app.showToast("请选择售后原因")
      return
    }
    let params = {
      applyQtys: this.data.quantity,
      image1: this.data.img_arr.length > 0 ? this.data.img_arr[0] : "",
      image2: this.data.img_arr.length > 1 ? this.data.img_arr[1] : "",
      image3: this.data.img_arr.length > 2 ? this.data.img_arr[2] : "",
      image4: this.data.img_arr.length > 3 ? this.data.img_arr[3] : "",
      orderInfoId: this.data.id,
      reasons: this.data.reason.name,
      refundType: 0,
      remark: this.data.details,
      serviceType: this.data.style.type
    }
    api.refundApply(params).then(res => {
      app.showToast("提交售后成功!")
      tt.redirectTo({
        url: '../after-sale-info/index?id=' + res
      })
    }).catch(msg => {
      app.showToast("提交售后失败!" + msg)
    })
  },
  bindReasonChange: function(e) {
    console.log(e)
    let that = this;
    that.setData({
      reason: {
        type: e.detail.value + 1,
        name: that.data.reasons[e.detail.value]
      }
    })
  },
  preview(e) {
    console.log(e)
    let that = this;
    tt.previewImage({
      current: e.currentTarget.dataset.url,
      urls: that.data.img_arr
    })
  },
  remove(e) {
    let that = this;
    let url = e.currentTarget.dataset.url
    tt.showModal({
      title: '删除图片',
      success: function(e) {
        if (e.confirm) {
          let x = -1;
          for (let i in that.data.img_arr) {
            if (that.data.img_arr[i] === url) {
              x = i;
              break;
            }
          }
          if (x >= 0) {
            that.data.img_arr.splice(x, 1);
            that.setData({
              img_arr: that.data.img_arr
            })
          }
        }
      }
    })
  },
  lower_count() {
    if (this.data.quantity > 1) {
      this.setData({
        quantity: this.data.quantity - 1
      })
      this.setAmount();
    } else {
      app.showToast("数量不能小于1")
    }
  },
  upper_count() {
    if (this.data.quantity < this.data.goods.goodsCount) {
      this.setData({
        quantity: this.data.quantity + 1
      })
      this.setAmount();
    } else {
      app.showToast("数量不能大于可售后数量:" + this.data.goods.goodsCount)
    }
  },
  setAmount() {
    this.setData({
      amount: this.data.quantity * this.data.goods.goodsPrice,
      amount_jb: 0
    })
  }
})